<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

import { getDataCount } from '@/api/system/statement'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      dataCount: [
        { value: 325, name: '业务量' },
        { value: 240, name: '信息量' },
        { value: 149, name: '数据量' }
      ],
      // 查询条件
      getMessageForm: {
        statementKey: undefined
      },
      getApplyForm: {
        statementKey: undefined
      },
      getBusinessForm: {
        statementKey: undefined
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },

  created() {
    this.getBusiDataCount()
    this.getMessageDataCount()
    this.getApplyDataCount()
  },
  methods: {
    // 获取业务数据
    getBusiDataCount() {
      this.getBusinessForm.statementKey = 'approveTotal'
      getDataCount(this.getBusinessForm).then(res => {
        // console.log('res ---------- ', res.data)
        const arr = res.data.map(item => item.dataCount)
        // console.log('arr ---------- ', arr)
        this.dataCount[0].value = arr
        // console.log('this.datacount[0] ----------', this.datacount[0])
        this.initChart()
      })
    },

    // 获取信息量
    getMessageDataCount() {
      this.getMessageForm.statementKey = 'noticeTotal'
      getDataCount(this.getMessageForm).then(res => {
        const arr = res.data.map(item => item.dataCount)
        // console.log('arr ---------- ', arr)
        this.dataCount[1].value = arr
        this.initChart()
      })
    },

    // 获取数据量
    getApplyDataCount() {
      this.getApplyForm.statementKey = 'applyTotal'
      getDataCount(this.getApplyForm).then(res => {
        const arr = res.data.map(item => item.dataCount)
        // console.log('arr ---------- ', arr)
        this.dataCount[2].value = arr
        this.initChart()
      })
    },

    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['业务量', '信息量', '数据量']
        },
        series: [
          {
            name: '报表数据',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: this.dataCount,
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
